package com.example.snackfood.controller;

import com.alibaba.fastjson2.JSON;
import com.example.snackfood.entity.Users;
import com.example.snackfood.services.IsnackFoodUsersService;
import com.example.snackfood.utils.LoginUserVo;
import com.example.snackfood.utils.R;
import com.example.snackfood.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @program: snackFood
 * @description:
 * @author: lydms
 * @create: 2023-11-22 21:08
 **/


@RestController
@RequestMapping("/snackFood")
public class SnackFoodUsersController {



    @Autowired
    private IsnackFoodUsersService snackFoodService;


    @Resource
    private RedisTemplate redisTemplate;


    @PostMapping("/login")
    public R login(@RequestBody LoginUserVo UserVo) {
        // 1.根据用户名去查询
        Users users = snackFoodService.selectUserByUserName(UserVo.getUsername());
        if (users == null) {
            return R.error(404, "用户不存在");
        }

        // 2.存在，判断密码对不对
        boolean ok = SecurityUtils.matchesPassword(UserVo.getPassword(),SecurityUtils.encryptPassword(users.getPassword()));
        if (ok) {
            String token = UUID.randomUUID().toString().replace("-", "");
            System.out.println(token);
            redisTemplate.opsForValue().set(token, JSON.toJSONString(users), 1, TimeUnit.HOURS);
            return R.success("成功登录").put("token", token);
        } else {
            return R.error("密码错误");
        }

    }
    @GetMapping("/get")
    public R getUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        String json = (String) redisTemplate.opsForValue().get(token);
        if (json == null) {
            return R.error("redis没有该用户信息");
        }
        Users sysUser = JSON.parseObject(json, Users.class);

        return R.success(sysUser);
    }

    /**
     * 增加用户
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    public R saveUser(@RequestBody Users user) {
        snackFoodService.saveUser(user);
        return R.success("成功添加");
    }

    /**
     * 查所有用户信息
     * @return
     */
    @GetMapping("/findAll")
    public List<Users> findAll(){
        return snackFoodService.findAll();
    }

    /**
     * 根据id查对应信息
     * @return
     */
    @GetMapping("/get/{id}")
    public Users get(@PathVariable Long id){
        return snackFoodService.get(id);
    }

    /**
     * 改用户信息
     */
    @PutMapping("/update")
    public R update(@RequestBody Users users){
        snackFoodService.update(users);
        return R.success("修改成功");
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable Long ids){
        snackFoodService.delete(ids);
        return R.success("删除用户成功");
    }
}
